﻿#include "furniturewidget.h"

FurnitureWidget::FurnitureWidget(QWidget *parent):
    QWidget(parent)
{
    fuTcpServer = FuTcpServer::getTcpServer();
    QLabel *placeLabel1 = new QLabel("楼道",this);
    QLabel *placeLabel2 = new QLabel("厨房",this);
    QLabel *placeLabel3 = new QLabel("餐厅",this);
    QLabel *placeLabel4 = new QLabel("卫生间",this);
    QLabel *placeLabel5 = new QLabel("次卧",this);
    QLabel *placeLabel6 = new QLabel("书房",this);
    QLabel *placeLabel7 = new QLabel("客厅",this);
    QLabel *placeLabel8 = new QLabel("走廊",this);
    QLabel *placeLabel9 = new QLabel("主卧",this);


    this->setFixedSize(Common::WIDTH,Common::HEIGHT);
    placeLabel1->move(10,10);
    placeLabel2->move(140,10);
    placeLabel3->move(340,10);
    placeLabel4->move(660,10);
    placeLabel5->move(860,10);
    placeLabel6->move(10,600);
    placeLabel7->move(360,600);
    placeLabel8->move(520,280);
    placeLabel9->move(760,600);

    kitchenLampLabel = new QLabel(this);
    kitchenLampLabel->setPixmap(QPixmap(":/images/off.png"));
    kitchenLampLabel->move(145,40);
    kitchenLampButton = new QPushButton("开",this);
    this->isKitchenLamp = false;
    kitchenLampButton->resize(70,40);
    kitchenLampButton->move(135,120);
    connect(kitchenLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_kitchenLamp()));

    kitchenVentilatorLabel = new QLabel(this);
    kitchenVentilatorLabel2 = new QLabel(this);
    kitchenVentilatorLabel->setPixmap(QPixmap(":/images/power.png"));
    kitchenVentilatorLabel2->setPixmap(QPixmap(":/images/抽油烟机.png"));
    kitchenVentilatorLabel2->move(245,10);
    kitchenVentilatorLabel2->resize(40,40);
    kitchenVentilatorLabel->move(240,50);
    kitchenVentilatorLabel->resize(60,70);
    kitchenVentilatorSelect = new QComboBox(this);
    kitchenVentilatorSelect->addItem("开启");
    kitchenVentilatorSelect->resize(100, 30);
    kitchenVentilatorSelect->move(220, 120);
    kitchenVentilatorButton = new QPushButton("确定",this);
    kitchenVentilatorButton->resize(70,40);
    kitchenVentilatorButton->move(240,150);
    connect(kitchenVentilatorButton,SIGNAL(clicked(bool)),this,SLOT(slot_kitchenVentilator()));

    diningRoomLampLabel = new QLabel(this);
    diningRoomLampLabel ->setPixmap(QPixmap(":/images/off.png"));
    diningRoomLampLabel ->move(345,40);
    diningRoomLampButton = new QPushButton("开",this);
    this->isDiningRoomLamp = false;
    diningRoomLampButton->resize(70,40);
    diningRoomLampButton->move(335,120);
    connect(diningRoomLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_diningRoomLamp()));

    toiletLampLabel = new QLabel(this);
    toiletLampLabel  ->setPixmap(QPixmap(":/images/off.png"));
    toiletLampLabel  ->move(665,40);
    toiletLampButton = new QPushButton("开",this);
    this->isToiletLamp = false;
    toiletLampButton->resize(70,40);
    toiletLampButton->move(655,120);
    connect(toiletLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_toiletLamp()));

    secondBedroomLampLabel = new QLabel(this);
    secondBedroomLampLabel ->setPixmap(QPixmap(":/images/off.png"));
    secondBedroomLampLabel ->move(865,40);
    secondBedroomLampButton = new QPushButton("开",this);
    this->isSecondBedroomLamp = false;
    secondBedroomLampButton->resize(70,40);
    secondBedroomLampButton->move(855,120);
    connect(secondBedroomLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_secondBedroomLamp()));

    studyRoomLampLabel = new QLabel(this);
    studyRoomLampLabel ->setPixmap(QPixmap(":/images/off.png"));
    studyRoomLampLabel ->move(15,470);
    studyRoomLampButton = new QPushButton("开",this);
    this->isStudyRoomLamp = false;
    studyRoomLampButton->resize(70,40);
    studyRoomLampButton->move(5,550);
    connect(studyRoomLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_studyRoomLamp()));

    studyRoomAirLabel = new QLabel(this);
    studyRoomAirLabel2 = new QLabel(this);
    studyRoomAirLabel->setPixmap(QPixmap(":/images/power.png"));
    studyRoomAirLabel2->setPixmap(QPixmap(":/images/空调.png"));
    studyRoomAirLabel->move(260,470);
    studyRoomAirLabel2->move(255,420);
    studyRoomAirLabel->resize(60,70);
    studyRoomAirLabel2->resize(60,60);
    studyRoomAirSelect = new QComboBox(this);
    studyRoomAirSelect->addItem("开启");
    studyRoomAirSelect->resize(100, 30);
    studyRoomAirSelect->move(240, 550);
    studyRoomAirButton = new QPushButton("确定",this);
    studyRoomAirButton->resize(70,40);
    studyRoomAirButton->move(260,580);
    connect(studyRoomAirButton,SIGNAL(clicked(bool)),this,SLOT(slot_studyRoomAir()));

    livingRoomLampLabel = new QLabel(this);
    livingRoomLampLabel ->setPixmap(QPixmap(":/images/off.png"));
    livingRoomLampLabel ->move(365,470);
    livingRoomLampButton = new QPushButton("开",this);
    this->isLivingRoomLamp = false;
    livingRoomLampButton->resize(70,40);
    livingRoomLampButton->move(355,550);
    connect(livingRoomLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_livingRoomLamp()));

    QLabel *livingRoomTempLabel = new QLabel("温度",this);
    livingRoomTempEdit = new QLineEdit(this);
    livingRoomTempLabel->move(500,460);
    livingRoomTempEdit->resize(70,40);
    livingRoomTempEdit->move(500,480);
    QLabel *livingRoomHumidityLabel = new QLabel("湿度",this);
    livingRoomHumidityEdit = new QLineEdit(this);
    livingRoomHumidityLabel->move(500,530);
    livingRoomHumidityEdit->resize(70,40);
    livingRoomHumidityEdit->move(500,550);

    livingRoomAirLabel = new QLabel(this);
    livingRoomAirLabel2 = new QLabel(this);
    livingRoomAirLabel->setPixmap(QPixmap(":/images/power.png"));
    livingRoomAirLabel2->setPixmap(QPixmap(":/images/客厅空调.png"));
    livingRoomAirLabel->move(660,470);
    livingRoomAirLabel2->move(665,410);
    livingRoomAirLabel->resize(60,70);
    livingRoomAirLabel2->resize(60,60);
    livingRoomAirSelect = new QComboBox(this);
    livingRoomAirSelect->addItem("开启");
    livingRoomAirSelect->resize(100, 30);
    livingRoomAirSelect->move(640, 550);
    livingRoomAirButton = new QPushButton("确定",this);
    livingRoomAirButton->resize(70,40);
    livingRoomAirButton->move(660,580);
    connect(livingRoomAirButton,SIGNAL(clicked(bool)),this,SLOT(slot_livingRoomAir()));

    masterBedroomLampLabel = new QLabel(this);
    masterBedroomLampLabel ->setPixmap(QPixmap(":/images/off.png"));
    masterBedroomLampLabel ->move(765,470);
    masterBedroomLampButton = new QPushButton("开",this);
    this->isMasterBedroomLamp = false;
    masterBedroomLampButton->resize(70,40);
    masterBedroomLampButton->move(755,550);
    connect(masterBedroomLampButton,SIGNAL(clicked(bool)),this,SLOT(slot_masterBedroomLamp()));

    QLabel *masterRoomTempLabel = new QLabel("温度",this);
    masterRoomTempEdit = new QLineEdit(this);
    masterRoomTempLabel->move(920,460);
    masterRoomTempEdit->resize(70,40);
    masterRoomTempEdit->move(920,480);
    QLabel *masterRoomHumidityLabel = new QLabel("湿度",this);
    masterRoomHumidityEdit = new QLineEdit(this);
    masterRoomHumidityLabel->move(920,530);
    masterRoomHumidityEdit->resize(70,40);
    masterRoomHumidityEdit->move(920,550);

    masterBedroomAirLabel = new QLabel(this);
    masterBedroomAirLabel2 = new QLabel(this);
    masterBedroomAirLabel->setPixmap(QPixmap(":/images/power.png"));
    masterBedroomAirLabel2->setPixmap(QPixmap(":/images/空调.png"));
    masterBedroomAirLabel->move(1100,470);
    masterBedroomAirLabel2->move(1100,420);
    masterBedroomAirLabel->resize(60,70);
    masterBedroomAirLabel2->resize(60,60);
    masterBedroomAirSelect = new QComboBox(this);
    masterBedroomAirSelect->addItem("开启");
    masterBedroomAirSelect->resize(100, 30);
    masterBedroomAirSelect->move(1080, 550);
    masterBedroomAirButton = new QPushButton("确定",this);
    masterBedroomAirButton->resize(70,40);
    masterBedroomAirButton->move(1100,580);
    connect(masterBedroomAirButton,SIGNAL(clicked(bool)),this,SLOT(slot_masterBedroomAir()));

    envTimer = new QTimer(this);
    connect(envTimer, &QTimer::timeout, this, &FurnitureWidget::requestEnvData);
    envTimer->start(5000); // 每5秒请求一次温湿度数据
}

void FurnitureWidget::paintEvent(QPaintEvent *event)
{
    double curWidth = this->width();
    double curHeight = this->height();
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::blue);
    pen.setWidth(5);
    painter.setPen(pen);
    painter.drawLine(0, 0, curWidth, 0);
    painter.drawLine(0, 0, 0, curHeight);
    painter.drawLine(curWidth, 0, curWidth, curHeight);
    painter.drawLine(0, curHeight, curWidth, curHeight);
    pen.setColor(Qt::black);
    painter.setPen(pen);
    painter.drawLine(130,0,130,220);
    painter.drawLine(130,200,330,200);
    painter.drawLine(330,0,330,80);
    painter.drawLine(330,180,330,200);
    painter.drawLine(650,0,650,200);
    painter.drawLine(650,200,750,200);
    painter.drawLine(850,0,850,200);
    painter.drawLine(830,200,870,200);
    painter.drawLine(1030,200,1060,200);
    painter.drawLine(1060,200,1060,320);
    painter.drawLine(1060,260,1200,260);
    painter.drawLine(1060,320,1030,320);
    painter.drawLine(750,320,750,620);
    painter.drawLine(750,320,850,320);
    painter.drawLine(750,320,750,320);
    painter.drawLine(130,300,130,320);
    painter.drawLine(0,320,200,320);
    painter.drawLine(330,320,350,320);
    painter.drawLine(350,320,350,620);

}

void FurnitureWidget::kitchenLampAfterOn(const QByteArray &ack)
{

    if(ack == "1")
    {
        if(!isKitchenLamp)
        {
            QMessageBox::information(this, "操作成功", "厨房灯已开启！");
            kitchenLampButton->setText("关");
            kitchenLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isKitchenLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "厨房灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制厨房灯失败,请检查设备！");
    }
    else
        return;

}
void FurnitureWidget::kitchenLampAfterOff(const QByteArray &ack)
{

    if(ack == "1")
    {
        if(isKitchenLamp)
        {
            qDebug() << this->fuTcpServer->command <<endl;
            QMessageBox::information(this, "操作成功", "厨房灯已关闭！");
            kitchenLampButton->setText("开");
            kitchenLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isKitchenLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "厨房灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制厨房灯失败,请检查设备！");
    }
    else
        return;

}
void FurnitureWidget::kitchenVentilatorAfter(const QByteArray &ack)
{
    if(ack == "1") {
        QString selected = kitchenVentilatorSelect->currentText();
        if(selected == "开启"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItems({"关闭", "低档", "高档", "灯光开启"});
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/choufeng.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机已开启！");
        }
        else if(selected == "关闭"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItem("开启");
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/power.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机已关闭！");
        }
        else if(selected == "低档"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItems({"关闭", "高档", "灯光开启"});
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/didang.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机低档已开启！");
        }
        else if(selected == "高档"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItems({"关闭", "低档", "灯光开启"});
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/gaodang.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机高档已开启！");
        }
        else if(selected == "灯光开启"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItems({"关闭", "低档", "高档", "灯光关闭"});
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/zhaoming.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机灯光已开启！");
        }
        else if(selected == "灯光关闭"){
            kitchenVentilatorSelect->clear();
            kitchenVentilatorSelect->addItems({"关闭", "低档", "高档", "灯光开启"});
            kitchenVentilatorLabel->setPixmap(QPixmap(":/images/choufeng.png"));
            QMessageBox::information(this, "操作成功", "抽油烟机灯光已关闭！");
        }
        else
            return;
    }
    else if(ack == "0") {
        QMessageBox::warning(this, "操作失败", "控制抽油烟机失败,请检查设备！");
    }
}

// 餐厅灯 - 开灯响应
void FurnitureWidget::diningRoomLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isDiningRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "餐厅灯已开启！");
            diningRoomLampButton->setText("关");
            diningRoomLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isDiningRoomLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "餐厅灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制餐厅灯失败,请检查设备！");
    }
}

// 餐厅灯 - 关灯响应
void FurnitureWidget::diningRoomLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isDiningRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "餐厅灯已关闭！");
            diningRoomLampButton->setText("开");
            diningRoomLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isDiningRoomLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "餐厅灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制餐厅灯失败,请检查设备！");
    }
}

// 卫生间灯 - 开灯响应
void FurnitureWidget::toiletLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isToiletLamp)
        {
            QMessageBox::information(this, "操作成功", "卫生间灯已开启！");
            toiletLampButton->setText("关");
            toiletLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isToiletLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "卫生间灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制卫生间灯失败,请检查设备！");
    }
}

// 卫生间灯 - 关灯响应
void FurnitureWidget::toiletLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isToiletLamp)
        {
            QMessageBox::information(this, "操作成功", "卫生间灯已关闭！");
            toiletLampButton->setText("开");
            toiletLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isToiletLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "卫生间灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制卫生间灯失败,请检查设备！");
    }
}

// 次卧灯 - 开灯响应
void FurnitureWidget::secondBedroomLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isSecondBedroomLamp)
        {
            QMessageBox::information(this, "操作成功", "次卧灯已开启！");
            secondBedroomLampButton->setText("关");
            secondBedroomLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isSecondBedroomLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "次卧灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制次卧灯失败,请检查设备！");
    }
}

// 次卧灯 - 关灯响应
void FurnitureWidget::secondBedroomLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isSecondBedroomLamp)
        {
            QMessageBox::information(this, "操作成功", "次卧灯已关闭！");
            secondBedroomLampButton->setText("开");
            secondBedroomLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isSecondBedroomLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "次卧灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制次卧灯失败,请检查设备！");
    }
}

// 书房灯 - 开灯响应
void FurnitureWidget::studyRoomLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isStudyRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "书房灯已开启！");
            studyRoomLampButton->setText("关");
            studyRoomLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isStudyRoomLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "书房灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制书房灯失败,请检查设备！");
    }
}

// 书房灯 - 关灯响应
void FurnitureWidget::studyRoomLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isStudyRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "书房灯已关闭！");
            studyRoomLampButton->setText("开");
            studyRoomLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isStudyRoomLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "书房灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制书房灯失败,请检查设备！");
    }
}

// 客厅灯 - 开灯响应
void FurnitureWidget::livingRoomLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isLivingRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "客厅灯已开启！");
            livingRoomLampButton->setText("关");
            livingRoomLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isLivingRoomLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "客厅灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制客厅灯失败,请检查设备！");
    }
}

// 客厅灯 - 关灯响应
void FurnitureWidget::livingRoomLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isLivingRoomLamp)
        {
            QMessageBox::information(this, "操作成功", "客厅灯已关闭！");
            livingRoomLampButton->setText("开");
            livingRoomLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isLivingRoomLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "客厅灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制客厅灯失败,请检查设备！");
    }
}

// 主卧灯 - 开灯响应
void FurnitureWidget::masterBedroomLampAfterOn(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(!isMasterBedroomLamp)
        {
            QMessageBox::information(this, "操作成功", "主卧灯已开启！");
            masterBedroomLampButton->setText("关");
            masterBedroomLampLabel->setPixmap(QPixmap(":/images/on.png"));
            isMasterBedroomLamp = true;
        }
        else
            QMessageBox::critical(this, "操作失败", "主卧灯已开启！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制主卧灯失败,请检查设备！");
    }
}

// 主卧灯 - 关灯响应
void FurnitureWidget::masterBedroomLampAfterOff(const QByteArray &ack)
{
    if(ack == "1")
    {
        if(isMasterBedroomLamp)
        {
            QMessageBox::information(this, "操作成功", "主卧灯已关闭！");
            masterBedroomLampButton->setText("开");
            masterBedroomLampLabel->setPixmap(QPixmap(":/images/off.png"));
            isMasterBedroomLamp = false;
        }
        else
            QMessageBox::critical(this, "操作失败", "主卧灯已关闭！");
    }
    else if(ack == "0")
    {
        QMessageBox::critical(this, "操作失败", "控制主卧灯失败,请检查设备！");
    }
}

void FurnitureWidget::studyRoomAirAfter(const QByteArray &ack)
{
    if(ack == "1") {
        QString selected = studyRoomAirSelect->currentText();
        if(selected == "开启"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItems({"关闭", "冷风", "热风", "除湿","送风"});
            studyRoomAirLabel->setPixmap(QPixmap(":/images/open.png"));
            QMessageBox::information(this, "操作成功", "书房空调已开启！");
        }
        else if(selected == "关闭"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItem("开启");
            studyRoomAirLabel->setPixmap(QPixmap(":/images/power.png"));
            QMessageBox::information(this, "操作成功", "书房空调已关闭！");
        }
        else if(selected == "冷风"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItems({"关闭", "热风", "除湿","送风"});
            studyRoomAirLabel->setPixmap(QPixmap(":/images/zhileng.png"));
            QMessageBox::information(this, "操作成功", "书房空调冷风已开启！");
        }
        else if(selected == "热风"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItems({"关闭", "冷风", "除湿","送风"});
            studyRoomAirLabel->setPixmap(QPixmap(":/images/zhire.png"));
            QMessageBox::information(this, "操作成功", "书房空调热风已开启！");
        }
        else if(selected == "除湿"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItems({"关闭", "冷风", "热风","送风"});
            studyRoomAirLabel->setPixmap(QPixmap(":/images/chushi.png"));
            QMessageBox::information(this, "操作成功", "书房空调除湿已开启！");
        }
        else if(selected == "送风"){
            studyRoomAirSelect->clear();
            studyRoomAirSelect->addItems({"关闭", "冷风", "热风", "除湿"});
            studyRoomAirLabel->setPixmap(QPixmap(":/images/songfeng.png"));
            QMessageBox::information(this, "操作成功", "书房空调送风已关闭！");
        }
        else
            return;
    }
    else if(ack == "0") {
        QMessageBox::warning(this, "操作失败", "控制书房空调失败,请检查设备！");
    }
}



void FurnitureWidget::livingRoomAirAfter(const QByteArray &ack)
{
    if(ack == "1") {
        QString selected = livingRoomAirSelect->currentText();
        if(selected == "开启"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItems({"关闭", "冷风", "热风", "除湿","送风"});
            livingRoomAirLabel->setPixmap(QPixmap(":/images/open.png"));
            QMessageBox::information(this, "操作成功", "客厅空调已开启！");
        }
        else if(selected == "关闭"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItem("开启");
            livingRoomAirLabel->setPixmap(QPixmap(":/images/power.png"));
            QMessageBox::information(this, "操作成功", "客厅空调已关闭！");
        }
        else if(selected == "冷风"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItems({"关闭", "热风", "除湿","送风"});
            livingRoomAirLabel->setPixmap(QPixmap(":/images/zhileng.png"));
            QMessageBox::information(this, "操作成功", "客厅空调冷风已开启！");
        }
        else if(selected == "热风"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItems({"关闭", "冷风", "除湿","送风"});
            livingRoomAirLabel->setPixmap(QPixmap(":/images/zhire.png"));
            QMessageBox::information(this, "操作成功", "客厅空调热风已开启！");
        }
        else if(selected == "除湿"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItems({"关闭", "冷风", "热风","送风"});
            livingRoomAirLabel->setPixmap(QPixmap(":/images/chushi.png"));
            QMessageBox::information(this, "操作成功", "客厅空调除湿已开启！");
        }
        else if(selected == "送风"){
            livingRoomAirSelect->clear();
            livingRoomAirSelect->addItems({"关闭", "冷风", "热风", "除湿"});
            livingRoomAirLabel->setPixmap(QPixmap(":/images/songfeng.png"));
            QMessageBox::information(this, "操作成功", "客厅空调送风已关闭！");
        }
        else
            return;
    }
    else if(ack == "0") {
        QMessageBox::warning(this, "操作失败", "控制客厅空调失败,请检查设备！");
    }
}




void FurnitureWidget::masterBedroomAirAfter(const QByteArray &ack)
{
    if(ack == "1") {
        QString selected = masterBedroomAirSelect->currentText();
        if(selected == "开启"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItems({"关闭", "冷风", "热风", "除湿","送风"});
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/open.png"));
            QMessageBox::information(this, "操作成功", "主卧空调已开启！");
        }
        else if(selected == "关闭"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItem("开启");
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/power.png"));
            QMessageBox::information(this, "操作成功", "主卧空调已关闭！");
        }
        else if(selected == "冷风"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItems({"关闭", "热风", "除湿","送风"});
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/zhileng.png"));
            QMessageBox::information(this, "操作成功", "主卧空调冷风已开启！");
        }
        else if(selected == "热风"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItems({"关闭", "冷风", "除湿","送风"});
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/zhire.png"));
            QMessageBox::information(this, "操作成功", "主卧空调热风已开启！");
        }
        else if(selected == "除湿"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItems({"关闭", "冷风", "热风","送风"});
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/chushi.png"));
            QMessageBox::information(this, "操作成功", "主卧空调除湿已开启！");
        }
        else if(selected == "送风"){
            masterBedroomAirSelect->clear();
            masterBedroomAirSelect->addItems({"关闭", "冷风", "热风", "除湿"});
            masterBedroomAirLabel->setPixmap(QPixmap(":/images/songfeng.png"));
            QMessageBox::information(this, "操作成功", "主卧空调送风已关闭！");
        }
        else
            return;
    }
    else if(ack == "0") {
        QMessageBox::warning(this, "操作失败", "控制主卧空调失败,请检查设备！");
    }
}

// 温湿度更新函数
void FurnitureWidget::updateLivingRoomTemp(const QString &temp)
{
    livingRoomTempEdit->setText(temp + "°C");
}

void FurnitureWidget::updateLivingRoomHumidity(const QString &humidity)
{
    livingRoomHumidityEdit->setText(humidity + "%");
}

void FurnitureWidget::updateMasterRoomTemp(const QString &temp)
{
    masterRoomTempEdit->setText(temp + "°C");
}

void FurnitureWidget::updateMasterRoomHumidity(const QString &humidity)
{
    masterRoomHumidityEdit->setText(humidity + "%");
}

void FurnitureWidget::requestEnvData()
{
    if(fuTcpServer->socket == nullptr) return;

    // 请求客厅温湿度
    QByteArray livingRoomTempCmd = QByteArray::number(Common::livingRoomTemperature);
    fuTcpServer->socket->write(livingRoomTempCmd);

    QByteArray livingRoomHumidityCmd = QByteArray::number(Common::livingRoomHumidity);
    fuTcpServer->socket->write(livingRoomHumidityCmd);

    // 请求主卧温湿度
    QByteArray masterRoomTempCmd = QByteArray::number(Common::masterBedroomTemperature);
    fuTcpServer->socket->write(masterRoomTempCmd);

    QByteArray masterRoomHumidityCmd = QByteArray::number(Common::masterBedroomHumidity);
    fuTcpServer->socket->write(masterRoomHumidityCmd);
}
void FurnitureWidget::slot_kitchenVentilator()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    QString select = kitchenVentilatorSelect->currentText();
    if(select == "开启"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "关闭"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "低档"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorLow);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "高档"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorHigh);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "灯光开启"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorLightOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "灯光关闭"){
        QByteArray command = QByteArray::number(Common::kitchenVentilatorLightClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_kitchenLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isKitchenLamp)
    {
        QByteArray command = QByteArray::number(Common::kitchenLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::kitchenLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_diningRoomLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isDiningRoomLamp)
    {
        QByteArray command = QByteArray::number(Common::diningRoomLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::diningRoomLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_toiletLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isToiletLamp)
    {
        QByteArray command = QByteArray::number(Common::toiletLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::toiletLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_secondBedroomLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isSecondBedroomLamp)
    {
        QByteArray command = QByteArray::number(Common::secondBedroomLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::secondBedroomLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_studyRoomLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isStudyRoomLamp)
    {
        QByteArray command = QByteArray::number(Common::studyRoomLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::studyRoomLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_studyRoomAir()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    QString select = studyRoomAirSelect->currentText();
    if(select == "开启"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "关闭"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "冷风"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerCool);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "热风"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerHeat);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "除湿"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerDehumidify);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "送风"){
        QByteArray command = QByteArray::number(Common::studyRoomAirConditionerVentilate);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_livingRoomLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isLivingRoomLamp)
    {
        QByteArray command = QByteArray::number(Common::livingRoomLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::livingRoomLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_livingRoomAir()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    QString select = livingRoomAirSelect->currentText();
    if(select == "开启"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "关闭"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "冷风"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerCool);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "热风"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerHeat);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "除湿"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerDehumidify);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "送风"){
        QByteArray command = QByteArray::number(Common::livingRoomAirConditionerVentilate);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_masterBedroomLamp()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    if(isMasterBedroomLamp)
    {
        QByteArray command = QByteArray::number(Common::masterBedroomLampClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else
    {
        QByteArray command = QByteArray::number(Common::masterBedroomLampOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

void FurnitureWidget::slot_masterBedroomAir()
{
    if(fuTcpServer->socket == nullptr)
    {
        QMessageBox::critical(nullptr,"错误","客户端未连接");
        return;
    }
    QString select = masterBedroomAirSelect->currentText();
    if(select == "开启"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerOpen);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "关闭"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerClose);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "冷风"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerCool);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "热风"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerHeat);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "除湿"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerDehumidify);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
    else if(select == "送风"){
        QByteArray command = QByteArray::number(Common::masterBedroomAirConditionerVentilate);
        qDebug() << "send command: " + command;
        fuTcpServer->socket->write(command);
    }
}

FurnitureWidget::~FurnitureWidget()
{

}
